/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Shop接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： ShopRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using NetCoreCMS.Common;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class ShopRepository : BaseRepository<Shop, Int32>, IShopRepository
    {
        public ShopRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption = options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public bool Pay(int user_id, int shop_id, decimal point, int leixing)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                decimal cash = 0;
                decimal shop = 0;
                switch (leixing)
                {
                    case 1://奖金
                        cash = point;
                        break;
                    case 2://积分
                        {
                            cash = point * 0.8m;
                            shop = point * 0.2m;
                        }
                        break;
                    case 3://分红
                        {

                        }
                        break;
                }
                //客户减钱，商铺加钱
                string strUpdate = "update [" + nameof(Users) + "] set cash_point-=" + cash + ",shop_point-=" + shop + " where id=" + user_id + " and cash_point>=" + cash + " and shop_point>=" + shop;
                strUpdate = strUpdate + ";update [" + nameof(shop) + "] set point+=" + point + " where id=" + shop_id;
                return _dbConnection.Execute(strUpdate) > 0;
            }
        }
        public bool Tixian(int shop_id, decimal point)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Shop) + "] set point-=" + point + " where id=" + shop_id + " and point>=" + point) > 0;
            }
        }

        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Shop) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
        public bool DeleteAll(int id, string env)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                //取得相册MODEL
                List<Models.Shop_albums> albumsList = _dbConnection.GetList<Models.Shop_albums>("where shop_id=@id", new { id = id }).ToList();

                //删除图片相册
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from [" + nameof(Shop_albums) + "] where shop_id=@shop_id;");
                //删除bills         
                strSql.Append("delete from [" + nameof(Shop_Bills) + "] where shop_id=@shop_id;");
                //删除apply
                strSql.Append("delete from [" + nameof(Shop_Apply) + "] where shop_id=@shop_id;");
                //删除主表
                strSql.Append("delete from [" + nameof(Shop) + "] where id=@shop_id");
                int rowsAffected = _dbConnection.Execute(strSql.ToString(), new { shop_id = id });
                if (rowsAffected > 0)
                {
                    //删除图片
                    foreach (Models.Shop_albums modelt in albumsList)
                    {
                        FileHelper.DeleteFile(modelt.thumb_path, env);
                        FileHelper.DeleteFile(modelt.original_path, env);
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}